﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using LiveTiss.Schemas;
using System.Windows.Data;

namespace LiveTiss.Guias.UserControls
{
    public partial class DiagnosticoSaidaInternacao : UserControl
    {
        #region Value

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
            typeof(ct_diagnosticoSaidaInternacao), typeof(DiagnosticoSaidaInternacao), new PropertyMetadata(null));

        public ct_diagnosticoSaidaInternacao Value
        {
            get { return (ct_diagnosticoSaidaInternacao)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        #endregion

        public DiagnosticoSaidaInternacao()
        {
            this.DataContext = this;

            if (this.Value == null)
            {
                this.Value = new ct_diagnosticoSaidaInternacao()
                {
                    diagnosticoPrincipal = new ct_diagnostico()
                    {
                        codigoDiagnostico = string.Empty,
                        descricaoDiagnostico = string.Empty,
                        nomeTabela = st_tabelasDiagnostico.CID10
                    },
                    diagnosticosSecundarios = new List<ct_diagnostico>(
                        new ct_diagnostico[]
                        {
                            new ct_diagnostico()
                            {
                                codigoDiagnostico = string.Empty,
                                descricaoDiagnostico = string.Empty,
                                nomeTabela = st_tabelasDiagnostico.CID10
                            }
                        }
                    ),
                    indicadorAcidente = st_indicadorAcidente.Item0,
                    indicadorAcidenteSpecified = false,
                    motivoSaidaInternacao = st_motivoSaida.Item11,
                    obito = new List<ct_obitoInternacao>()
                };
            }

            InitializeComponent();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (this.Value.diagnosticosSecundarios.Count < 3)
            {
                var list = this.Value.diagnosticosSecundarios.GetRange(0, this.Value.diagnosticosSecundarios.Count);

                list.Add(new ct_diagnostico()
                {
                    codigoDiagnostico = string.Empty,
                    descricaoDiagnostico = string.Empty,
                    nomeTabela = st_tabelasDiagnostico.CID10,
                });

                this.Value.diagnosticosSecundarios = list;
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            ContentControl ctrl = sender as ContentControl;

            var diagnosticoRemovido = ctrl.DataContext as ct_diagnostico;

            if (this.Value.diagnosticosSecundarios.Count > 1)
            {
                var list = this.Value.diagnosticosSecundarios.GetRange(0, this.Value.diagnosticosSecundarios.Count);

                list.Remove(diagnosticoRemovido);

                this.Value.diagnosticosSecundarios = list;
            }
        }

        private void btnAddObito_Click(object sender, RoutedEventArgs e)
        {
            var list = this.Value.obito.GetRange(0, this.Value.obito.Count);

            list.Add(new ct_obitoInternacao()
            {
                CID = new ct_diagnostico()
                {
                    codigoDiagnostico = string.Empty,
                    descricaoDiagnostico = string.Empty,
                    nomeTabela = st_tabelasDiagnostico.CID10
                },
                numeroDeclaracao = string.Empty
            });

            this.Value.obito = list;
        }

        private void btnDeleteObito_Click(object sender, RoutedEventArgs e)
        {
            ContentControl ctrl = sender as ContentControl;

            var obitoRemovido = ctrl.DataContext as ct_obitoInternacao;

            var list = this.Value.obito.GetRange(0, this.Value.obito.Count);

            list.Remove(obitoRemovido);

            this.Value.obito = list;
        }

        private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            var CIDUserControl = (sender as FrameworkElement).FindName("CIDUserControl") as CID;

            var current = (sender as FrameworkElement).DataContext as ct_diagnostico;

            Binding b = new Binding();

            b.Path = new PropertyPath("diagnostico");
            b.Mode = BindingMode.TwoWay;
            b.ValidatesOnDataErrors = true;
            b.NotifyOnValidationError = true;
            b.Source = current;

            CIDUserControl.SetBinding(CID.ValueProperty, b);
        }

        private void StackPanel_Loaded_1(object sender, RoutedEventArgs e)
        {
            var ucObitoInternacao = (sender as FrameworkElement).FindName("ucObitoInternacao") as ObitoInternacao;

            var current = (sender as FrameworkElement).DataContext as ct_obitoInternacao;

            Binding b = new Binding();

            b.Path = new PropertyPath("obito");
            b.Mode = BindingMode.TwoWay;
            b.ValidatesOnDataErrors = true;
            b.NotifyOnValidationError = true;
            b.Source = current;

            ucObitoInternacao.SetBinding(ObitoInternacao.ValueProperty, b);
        }
    }
}
